package com.kexinde.wjifen.action.usercenter;

import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.kexinde.wjifen.dao.ShopPointDao;
import com.kexinde.wjifen.dao.UserDao;
import com.kexinde.wjifen.model.User;
import com.kexinde.wjifen.util.MD5;
import com.kexinde.wjifen.vo.MyBaseInfo;
import com.kexinde.wjifen.vo.SafeSetting;
import com.opensymphony.xwork2.ActionSupport;

public class BaseInfoAction extends ActionSupport implements RequestAware,
		SessionAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final static Logger logger = Logger
			.getLogger(BaseInfoAction.class);

	private Map<String, Object> request;
	private Map<String, Object> session;
	
	private SafeSetting safeSetting;

	/**
	 * 进入个人中心首页
	 * 我的基本信息
	 * @return
	 */
	public String showUserCenter() { // 未完善
		try {
			User user = (User) session.get("user");
			if (user != null) { // 已经登录
				ShopPointDao shopPointDao = new ShopPointDao();
				MyBaseInfo myBaseInfo = new MyBaseInfo();
				myBaseInfo.setId(user.getId());
				myBaseInfo.setName(user.getName());
				myBaseInfo.setPic(user.getImagePath());
				myBaseInfo.setTotalPoint(user.getTotalPoint());
				myBaseInfo.setShopAmount(shopPointDao.
						countShopPointAmount(user.getId()));
				request.put("myBaseInfo", myBaseInfo);
				return SUCCESS;
			} else {
				return ERROR;
			}
		} catch (Exception ex) {
			logger.error(ex);
		}
		return ERROR;
	}

	
	/**
	 * 显示基本信息修改页面
	 * @return
	 */
	public String showBaseInfo() { // 未完善
		try {
			User user = (User) session.get("user");
			if (user != null) { // 已经登录
				return SUCCESS;
			} else {
				return ERROR;
			}
		} catch (Exception ex) {
			logger.error(ex);
		}
		return ERROR;
	}
	
	/**
	 * 基本信息修改页面的提交按钮
	 * @return
	 */
	public String modifyBaseInfo() { // 未完善
		try {
			User user = (User) session.get("user");
			if (user != null) { // 已经登录
				return SUCCESS;
			} else {
				return ERROR;
			}
		} catch (Exception ex) {
			logger.error(ex);
		}
		return ERROR;
	}
	
	
	/**
	 * 显示收货地址页面
	 * @return
	 */
	public String showReceivedAddress(){// 未完善
		try {
			User user = (User) session.get("user");
			if (user != null) { // 已经登录
				return SUCCESS;
			} else {
				return ERROR;
			}
		} catch (Exception ex) {
			logger.error(ex);
		}
		return ERROR;
	}
	
	
	/**
	 * 收货地址页面的提交按钮
	 * @return
	 */
	public String modifyReceivedAddress(){// 未完善
		try {
			User user = (User) session.get("user");
			if (user != null) { // 已经登录
				return SUCCESS;
			} else {
				return ERROR;
			}
		} catch (Exception ex) {
			logger.error(ex);
		}
		return ERROR;
	}
	
	
	
	/**
	 * 显示安全设置页面
	 * @return
	 */
	public String showSafeSetting(){// 未完善
		try {
			User user = (User) session.get("user");
			if (user != null) { // 已经登录
				return SUCCESS;
			} else {
				return ERROR;
			}
		} catch (Exception ex) {
			logger.error(ex);
		}
		return ERROR;
	}
	
	
	
	/**
	 * 安全设置页面的提交按钮
	 * @return
	 */
	public String modifySafeSetting(){
		try {
			User user = (User) session.get("user");
			if (user != null) { // 已经登录
				String oldPwd = MD5.encryption(this.safeSetting.getOldPwd());//对输入密码加密
				if(user.getPassword().equals(oldPwd)){//旧密码正确
					if(this.safeSetting.getNewPwd().equals(this.safeSetting.getReNewPwd())){//确认密码一致
						UserDao userDao = new UserDao();
						String newPwd = MD5.encryption(this.safeSetting.getNewPwd());
						
						if(userDao.modifyPassword(user.getId(), newPwd)){//修改密码成功
							
							user = userDao.findByName(user.getName());
							session.put("user", user);
							request.put("feedbackMsg", "修改密码成功");
							return SUCCESS;
							
						}else{	//修改密码失败
							request.put("feedbackMsg", "修改密码失败");
							return ERROR;
						}
							
						
					}else{//确认密码不一致
						request.put("feedbackMsg", "确认密码不一致");
						return ERROR;
					}
					
				}else{	//旧密码错误
					request.put("feedbackMsg", "原始密码错误");
					return ERROR;
				}
				
			} else {
				return ERROR;
			}
		} catch (Exception ex) {
			logger.error(ex);
		}
		return ERROR;
	}
	
	
	
	/**
	 * 显示手机管理页面
	 * @return
	 */
	public String showTelphoneManagement(){// 未完善
		try {
			User user = (User) session.get("user");
			if (user != null) { // 已经登录
				return SUCCESS;
			} else {
				return ERROR;
			}
		} catch (Exception ex) {
			logger.error(ex);
		}
		return ERROR;
	}
	
	
	
	/**
	 * 手机管理页面的提交按钮
	 * @return
	 */
	public String modifyTelphoneManagement(){// 未完善
		try {
			User user = (User) session.get("user");
			if (user != null) { // 已经登录
				return SUCCESS;
			} else {
				return ERROR;
			}
		} catch (Exception ex) {
			logger.error(ex);
		}
		return ERROR;
	}
	
	
	
	/**
	 * 显示修改头像页面
	 * @return
	 */
	public String showChangePic(){// 未完善
		try {
			User user = (User) session.get("user");
			if (user != null) { // 已经登录
				return SUCCESS;
			} else {
				return ERROR;
			}
		} catch (Exception ex) {
			logger.error(ex);
		}
		return ERROR;
	}
	
	
	
	/**
	 * 修改头像页面的提交按钮
	 * @return
	 */
	public String modifyChangePic(){// 未完善
		try {
			User user = (User) session.get("user");
			if (user != null) { // 已经登录
				return SUCCESS;
			} else {
				return ERROR;
			}
		} catch (Exception ex) {
			logger.error(ex);
		}
		return ERROR;
	}
	
	
	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}


	public SafeSetting getSafeSetting() {
		return safeSetting;
	}


	public void setSafeSetting(SafeSetting safeSetting) {
		this.safeSetting = safeSetting;
	}

}
